package com.bupt.ilink.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.validation.constraints.NotEmpty;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.UUID;

@Data
@TableName("tb_user")
public class User implements UserDetails {
    @TableId
    //private String userId = UUID.randomUUID().toString();
    private String userId;

    @NotEmpty
    private String username;

    @NotEmpty
    private String password;

    private String schoolName;

    private String schoolId;

    private String nickname;

    private String email;

    private Timestamp createdAt;
    //    @TableField("userIntroduction")
    private String userIntroduction;
    //    @TableField("userImage")
    private String userImage;
    //    @TableField("userCollege")
    private String userCollege;
    private String realname;
    private String sex;
    //    private String userStudentNumber;
    @TableField("userGrade")
    private String userGrade;
    @TableField("userMajor")
    private String userMajor;
    @TableField("userMobile")
    private String userMobile;
    @TableField("userCover")
    private String userCover;
    private String userType;
    @TableField("last_login_at")
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
    private Date lastLoginAt;



    public User() {
    }

    public User(@NotEmpty String username, @NotEmpty String password) {
        this.username = username;
        this.password = password;
    }

    /**
     * 授予不同用户访问权限
     * @return java.util.Collection<? extends org.springframework.security.core.GrantedAuthority>
     * @author liuzheng
     * @create 2024-07-27
     **/
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        Collection<GrantedAuthority> authorities = new ArrayList<>();
        SimpleGrantedAuthority authority = new SimpleGrantedAuthority("write");
        SimpleGrantedAuthority authorit = new SimpleGrantedAuthority("read");
        authorities.add(authority);
        authorities.add(authorit);
        return authorities;

    }


    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
